package com.web;

import com.domain.Employee;
import com.mysql.cj.xdevapi.Session;
import com.service.employeeService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * 员工的登录
 * 接收 loginEmployee.jsp 页面的 phone、 password数据
 * 提交到 employeeService 的 login 函数，查询员工数据
 * 重定向到 /showUser
 */
@WebServlet("/loginEmployee")
public class LoginEmployeeServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.解决请求乱码，解决响应乱码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        //2.获取数据
        String phone = request.getParameter("phone");
        String password = request.getParameter("password");
        System.out.println(phone +"员工在登录");
        //3.比较
        employeeService employeeService = new employeeService();
        Employee employee = employeeService.login(phone, password);
        //4.登录信息展示
        PrintWriter responseWriter = response.getWriter();
        if( employee != null){
            //登陆成功
            //重定向到主页面
            response.sendRedirect(request.getContextPath()+"/showUser");//重定向
            System.out.println(employee.getEmployeeName()+"员工登录成功");
        }else{
            //登录失败"
            responseWriter.write("该用户名密码不正确<a href=\"loginEmployee.jsp\">重新登录</a>");
            System.out.println(phone+"用户登录失败");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
